package com.caoustc.okhttplib.okhttp.platform

import android.annotation.SuppressLint
import com.caoustc.okhttplib.okhttp.OkHttpRequestParams
import com.caoustc.okhttplib.okhttp.Part
import com.caoustc.okhttplib.utils.ListUtils
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by cz on 2018/3/12.
 */
object OkHttpParamsHelper {
    @SuppressLint("SimpleDateFormat")
    private fun DateTime2Str(date: Date): String {
        return SimpleDateFormat("yyyyMMddHHmmss").format(date)
    }

    /**
     * 平台的 key sid
     */
    private var COMMON_AKEY = "S107-00000001"
    private var COMMON_ASID = "c2e7b547b11bc4eda02e88b3648f9e72"
    fun setCommonAkey(commonAkey: String) {
        COMMON_AKEY = commonAkey
    }

    fun setCommonAsid(commonAsid: String) {
        COMMON_ASID = commonAsid
    }

    /*protected static OkHttpRequestParams getBaseOkHttpParams(String method) {
        OkHttpRequestParams params = new OkHttpRequestParams();
        params.addBodyParameter("_aid", "S107");
        params.addBodyParameter("_sm", "md5");
        params.addBodyParameter("_requestMode", "post");
        params.addBodyParameter("_version", "v1");
        params.addBodyParameter("_timestamp", DateTime2Str(new Date()));
        params.addBodyParameter("_mt", method);
        params.addBodyParameter("_akey", COMMON_AKEY);
        return params;
    }*/
    @kotlin.jvm.JvmStatic
    fun addBaseOkHttpParams(params: OkHttpRequestParams?, method: String?): OkHttpRequestParams? {
        if (params != null) {
            params.addBodyParameterFront(PlatformConstants.AID, "S107")
            params.addBodyParameterFront(PlatformConstants.SM, "md5")
            params.addBodyParameterFront(PlatformConstants.REQUEST_MODE, "post")
            params.addBodyParameterFront(PlatformConstants.VERSION, "v1")
            params.addBodyParameterFront(PlatformConstants.TIMESTAMP, DateTime2Str(Date()))
            params.addBodyParameterFront(PlatformConstants.MT, method)
            params.addBodyParameterFront(PlatformConstants.AKEY, COMMON_AKEY)
        }
        return params
    }

    @kotlin.jvm.JvmStatic
    fun sortSign(params: OkHttpRequestParams?): OkHttpRequestParams? {
        if (params != null) {
            val sb = StringBuilder()
            sb.append(COMMON_ASID)
            var signMode = PlatformConstants.sign_method_MD5
            val partList = params.formParams
            if (!ListUtils.isEmpty(partList)) {
                Collections.sort(partList, PartComparator())
                for (part in partList!!) {
                    if (PlatformConstants.SM == part?.key) {
                        signMode = part.value.toString()
                    }
                    sb.append(part?.key).append(part?.value)
                }
            }
            sb.append(COMMON_ASID)
            val sign = SignUtils.sign(signMode, sb.toString())
            params.addBodyParameter(PlatformConstants.SIG, sign)
        }
        return params
    }
}